#include "teststdafx.h"
#include "stdafx.h"
#include <gtest/gtest.h>

#include "Business/Dealing.h"
#include "Business/Customer.h"

class CustomerPositionTest : public testing::Test
{
protected:

	virtual void SetUp()
	{
	}

	virtual void TearDown()
	{

	}
};

TEST_F(CustomerPositionTest, PositionValuePlusTest)
// void foo()
{
	PositionValue value;

	PositionValue plusValue(1000,10);
	value.Plus(plusValue);

	EXPECT_EQ(10,value.GetQuantity());
	EXPECT_DOUBLE_EQ(1000.00,value.GetPrice());

	PositionValue plusValue2(1100,10);
	value.Plus(plusValue2);

	EXPECT_EQ(20,value.GetQuantity());
	EXPECT_DOUBLE_EQ(1050.00,value.GetPrice());
}

TEST_F(CustomerPositionTest, PositionValueSubTest)
// void foo()
{
	PositionValue value;

	PositionValue plusValue(1000,10);
	value.Plus(plusValue);

	EXPECT_EQ(10,value.GetQuantity());
	EXPECT_DOUBLE_EQ(1000.00,value.GetPrice());

	PositionValue plusValue2(1100,10);
	value.Plus(plusValue2);

	EXPECT_EQ(20,value.GetQuantity());
	EXPECT_DOUBLE_EQ(1050.00,value.GetPrice());

	value.Sub(10);

	EXPECT_EQ(10,value.GetQuantity());
	EXPECT_DOUBLE_EQ(1050.00,value.GetPrice());

	value.Sub(10);

	EXPECT_EQ(0,value.GetQuantity());
	EXPECT_DOUBLE_EQ(0,value.GetPrice());

	PositionValue plusValue3(1200,10);
	value.Plus(plusValue3);

	EXPECT_EQ(10,value.GetQuantity());
	EXPECT_DOUBLE_EQ(1200.00,value.GetPrice());
}

TEST_F(CustomerPositionTest, OnOpenTradeTest)
// void foo()
{
	CustomerPosition position;
	{
		Order order = {0};
		order.contract = 0;
		order.direction = Direction::BUY;
		order.operation = Operation::OPEN;
		order.price = 1000.00;
		order.quantity = 10;
		Customer customer("","","");
		Dealing dealing(customer,order);
		Trade trade(dealing,1000.00,1,"SystemNo");	
		position.OnTrade(trade);

		PositionValue const& value = position.Get(PositionKey(order.contract,order.direction,Operation::OFFSET_TODAY));
		EXPECT_EQ(1,value.GetQuantity());
		EXPECT_DOUBLE_EQ(1000.00,value.GetPrice());

		Trade trade1(dealing,1100.00,1,"SystemNo");	
		position.OnTrade(trade1);
		EXPECT_EQ(2,value.GetQuantity());
		EXPECT_DOUBLE_EQ(1050.00,value.GetPrice());
	}
	{
		Order order = {0};
		order.contract = 0;
		order.direction = Direction::SELL;
		order.operation = Operation::OPEN;
		order.price = 1000.00;
		order.quantity = 10;
		Customer customer("","","");
		Dealing dealing(customer,order);
		Trade trade(dealing,1100.00,2,"SystemNo");	
		position.OnTrade(trade);

		PositionValue const& value = position.Get(PositionKey(order.contract,order.direction,Operation::OFFSET_TODAY));
		EXPECT_EQ(2,value.GetQuantity());
		EXPECT_DOUBLE_EQ(1100.00,value.GetPrice());

		Trade trade1(dealing,1200.00,1,"SystemNo");	
		position.OnTrade(trade1);
		EXPECT_EQ(3,value.GetQuantity());
		EXPECT_DOUBLE_EQ(3400.00/3.00,value.GetPrice());

	}
}


TEST_F(CustomerPositionTest, OnOffsetTodayTradeTest)
// void foo()
{
	CustomerPosition position;
	{
		Order order = {0};
		order.contract = 0;
		order.direction = Direction::BUY;
		order.operation = Operation::OPEN;
		order.price = 1000.00;
		order.quantity = 10;
		Customer customer("","","");
		Dealing dealing(customer,order);
		Trade trade(dealing,1000.00,1,"SystemNo");	
		position.OnTrade(trade);

		PositionValue const& value = position.Get(PositionKey(order.contract,order.direction,Operation::OFFSET_TODAY));
		EXPECT_EQ(1,value.GetQuantity());
		EXPECT_DOUBLE_EQ(1000.00,value.GetPrice());

		Trade trade1(dealing,1100.00,1,"SystemNo");	
		position.OnTrade(trade1);
		EXPECT_EQ(2,value.GetQuantity());
		EXPECT_DOUBLE_EQ(1050.00,value.GetPrice());
	}
	{
		Order order = {0};
		order.contract = 0;
		order.direction = Direction::SELL;
		order.operation = Operation::OFFSET_TODAY;
		order.price = 1000.00;
		order.quantity = 10;
		Customer customer("","","");
		Dealing dealing(customer,order);
		Trade trade(dealing,1100.00,1,"SystemNo");	
		position.OnTrade(trade);

		PositionValue const& value = position.Get(PositionKey(order.contract,Direction::BUY,Operation::OFFSET_TODAY));
		EXPECT_EQ(1,value.GetQuantity());
		EXPECT_DOUBLE_EQ(1050.00,value.GetPrice());
	}
	{
		Order order = {0};
		order.contract = 0;
		order.direction = Direction::SELL;
		order.operation = Operation::OFFSET_TODAY;
		order.price = 1000.00;
		order.quantity = 10;
		Customer customer("","","");
		Dealing dealing(customer,order);
		Trade trade(dealing,1200.00,1,"SystemNo");	
		position.OnTrade(trade);

		PositionValue const& value = position.Get(PositionKey(order.contract,Direction::BUY,Operation::OFFSET_TODAY));
		EXPECT_EQ(0,value.GetQuantity());
		EXPECT_DOUBLE_EQ(0,value.GetPrice());
	}
}
